package oa.base.web.Action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.base.util.json.JsonUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction<T> extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3384820165304269902L;
	/**
	 * 默认的
	 */
	public static final int DEFAULT_PAGE_NO = 1;
	public static final int DEFAULT_PAGE_SIZE = 20;
	
	
	
	protected Map<String,Object> putKey = new HashMap<String, Object>();
	
	protected Map<String,Object> linkMap = new HashMap<String, Object>();
	
	protected Map<String,Object> shortMap = new HashMap<String, Object>();
	
	protected Map<String,Object> filterMap = new HashMap<String, Object>();
	
	/**
	 * JSON 返回数据工具
	 */
	protected JsonUtils jsonUtils = new JsonUtils();
	
	public Map<String, Object> getPutKey() {
		return putKey;
	}

	public void setPutKey(Map<String, Object> putKey) {
		this.putKey = putKey;
	}
	
	

	public Map<String, Object> getLinkMap() {
		return linkMap;
	}

	public void setLinkMap(Map<String, Object> linkMap) {
		this.linkMap = linkMap;
	}

	public Map<String, Object> getShortMap() {
		return shortMap;
	}

	public void setShortMap(Map<String, Object> shortMap) {
		this.shortMap = shortMap;
	}

	public Map<String, Object> getFilterMap() {
		return filterMap;
	}

	public void setFilterMap(Map<String, Object> filterMap) {
		this.filterMap = filterMap;
	}



	public T entity;
	
	
	public String url;
	
	protected String sids;
	
	
	public String id;
	
	public String text;
	
	
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}
	/**
	 * 取得session
	 * @return
	 */
	protected HttpSession getSession(){
		return ServletActionContext.getRequest().getSession();
	}
	/**
	 * 实现此方法,返回对应的实体对象
	 * 
	 * @return
	 */
	protected abstract Class<T> getEntityClass();
	/**
	 * 取得HttpServletRequest
	 * @return
	 */
	protected HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	/**
	 * 构造方法
	 */
	public BaseAction() {
		super();
		try {
			this.entity = this.getEntityClass().newInstance();
		} catch (InstantiationException e) {
			e.getStackTrace();
		} catch (IllegalAccessException e) {
			e.getStackTrace();
		}
	}
	/**
	 * 分页大小
	 */
	private int pageSize;
	/**
	 * 当前页数
	 */
	private int pageNo;
	
	public int getPageNo() {
		return pageNo == 0 ? DEFAULT_PAGE_NO : pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize == 0 ? DEFAULT_PAGE_SIZE : pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	
	 /**
		 * json返回的值
		 */
		protected Object jsonResult;
	    /**
	     * 返回JSON
	     */
	    protected static final String JSON = null;
	    
	    private String skipHtml;
	    
	    /**
		 * @return 字段 skipHtml 的值
		 */
		public String getSkipHtml() {
			return skipHtml;
		}

		/**
		 * @param 将 skipHtml 设置到字段 skipHtml 上
		 */
		public void setSkipHtml(String skipHtml) {
			this.skipHtml = skipHtml;
		}

		public Object getJsonResult() {
			return jsonResult;
		}

		public void setJsonResult(Object jsonResult) {
			this.jsonResult = jsonResult;
		}
		
	    /**
	     * 绕过Template,直接输出内容的简便函数. 
	     */
	    protected String render(String text, String contentType) {
	    	 HttpServletResponse response = null;
	        try {
	        	//不需要返回
	        	if("0".equals(skipHtml)){
	        		return JSON;
	        	}else if("1".equals(skipHtml)){
		           
	            }else{
	            	 text = text.replace("&", "&amp;");
			         text = text.replace("<", "&lt");
			         text = text.replace(">", "&gt");
	            }
	        	response = ServletActionContext.getResponse();
	            response.setContentType(contentType);
		            
	            response.getWriter().write(text);
	            response.getWriter().close();
	        } catch (IOException e) {
	        	e.printStackTrace();
	        }finally{
	        	if(null != response){
	        		try {
						response.getWriter().close();
					} catch (IOException e) {
						e.printStackTrace();
					}
	        		response = null;
	        	}
	        }
	        return JSON;
	    }

	    /**
	     * 直接输出字符串.
	     */
	    protected String renderText(String text) {
	        return render(text, "text/plain;charset=UTF-8");
	    }

	    /**
	     * 直接输出HTML.
	     */
	    protected String renderHtml(String html) {
	        return render(html, "text/html;charset=UTF-8");
	    }

	    /**
	     * 直接输出XML.
	     */
	    protected String renderXML(String xml) {
	        return render(xml, "text/xml;charset=UTF-8");
	    }
	    
	    /**
	     * 直接输出Json.
	     */
	    protected String renderJson(String json) {
	        return render(json, "application/json;charset=UTF-8");
	    }
	    
	    /**
	     * ------------------------------------------------------------
	     * JSON 处理输出区
	     * ------------------------------------------------------------
	     */
	    /**
	     * 直接输出Json.
	     */
	    protected String renderJsonSuccess(String result) {
	    	 if("".equals(result)){
	    		 result = "\"\"";
	    	 }
	    	 this.jsonResult = "{\"success\":true,\"msg\":\"操作成功\", \"result\":" + result +"}";
	    	 renderJson(String.valueOf(jsonResult));
	    	 return JSON;
	    }
	    
	    protected String rederTypeJsonSuccess(String result){
	    	if("".equals(result)){
	    		 result = "\"\"";
	    	 }
	    	 this.jsonResult = "{\"success\":true,\"msg\":\"操作成功\", \"result\":" + result +"}";
	    	 renderHtml(String.valueOf(jsonResult));
	    	 return JSON;
	    }
	    protected String rederImgJsonSuccess(String result,String url){
	    	if("".equals(result)){
	    		 result = "\"\"";
	    	 }
	    	 this.jsonResult = "{\"success\":true,\"error\":\"0\",\"url\":\""+url+"\",\"msg\":\"操作成功\", \"result\":" + result +"}";
	    	 renderHtml(String.valueOf(jsonResult));
	    	 return JSON;
	    }
		/**
		 * 直接输出Json.
		 * @return
		 */
	    protected String renderJsonSuccess(){
	    	return this.renderJsonSuccess("");
	    }
		/**
	     * 直接输出Json.
	     */
	    protected String renderJsonError(Throwable e) {
	    	e.printStackTrace();
			this.renderJsonError(e.getMessage());
			return JSON;
	    }
	    /**
	     * 直接输出Json.
	     */
	    protected String renderJsonError(String error) {
			 this.jsonResult = "{\"success\":false,\"msg\":\"" + error + "\"}";
			 renderJson(String.valueOf(jsonResult));
	    	 return JSON;
	    }
	    
	    /**
	     * 直接输出Json.超时
	     */
	    protected String renderJsonTimeOut(String error) {
			 this.jsonResult = "{\"success\":false,\"msg\":\"" + error + "\"}";
			 renderJson(String.valueOf(jsonResult));
	    	 return JSON;
	    }
	    
	    public void afterinit() {
			
		}
	    public void beforeInit() {
			
		}
	    
	    public String index() {
	    	this.beforeInit();
	    	this.afterinit();
			return "index";
		}

		public String getSids() {
			return sids;
		}

		public void setSids(String sids) {
			this.sids = sids;
		}
	    
	   
	   
}
